//
//  ZallBaseTableViewCell.m
//  ZJS
//
//  Created by so on 16/5/10.
//  Copyright © 2016年 pengl. All rights reserved.
//

#import "ZallBaseTableViewCell.h"
#import "UIView+ZallAdditions.h"
#import "ZallGlobal.h"

@interface ZallBaseTableViewCell ()
@property (strong, nonatomic, readonly) UIView *topLineView;
@property (strong, nonatomic, readonly) UIView *bottomLineView;
@end

@implementation ZallBaseTableViewCell
@synthesize gap = _gap;
@synthesize contentInsets = _contentInsets;
@synthesize topLineView = _topLineView;
@synthesize bottomLineView = _bottomLineView;

#pragma mark - life cycle
- (instancetype)init {
    return ([self initWithFrame:CGRectZero]);
}

- (instancetype)initWithFrame:(CGRect)frame {
    return ([self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]);
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        _gap = CGSizeZero;
        _contentInsets = UIEdgeInsetsZero;
        _showTopLine = _showBottomLine = NO;
        _topHeadIndentationWidth = _topTailIndentationWidth = 0.0f;
        _bottomHeadIndentationWidth = _bottomTailIndentationWidth = 0.0f;
    }
    return (self);
}
#pragma mark -

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    if(_topLineView) {
        _topLineView.left = _topHeadIndentationWidth;
        _topLineView.top = 0;
        _topLineView.width = self.width - (_topHeadIndentationWidth + _topTailIndentationWidth);
        [self bringSubviewToFront:_topLineView];
    }
    if(_bottomLineView) {
        _bottomLineView.left = _bottomHeadIndentationWidth;
        _bottomLineView.bottom = self.height;
        _bottomLineView.width = self.width - (_bottomHeadIndentationWidth + _bottomTailIndentationWidth);
        [self bringSubviewToFront:_bottomLineView];
    }
}
#pragma mark -

#pragma mark - override
- (void)prepareForReuse {
    [super prepareForReuse];
    //_gap = CGSizeZero;
    //_contentInsets = UIEdgeInsetsZero;
    //_topHeadIndentationWidth = _bottomHeadIndentationWidth = 0;
    //_showTopLine = _showBottomLine = NO;
}
#pragma mark -

#pragma mark - setter
- (void)setContentInsets:(UIEdgeInsets)contentInsets {
    [self willChangeValueForKey:@"contentInsets"];
    _contentInsets = contentInsets;
    [self didChangeValueForKey:@"contentInsets"];
    [self setNeedsLayout];
}

- (void)setShowTopLine:(BOOL)showTopLine {
    [self willChangeValueForKey:@"showTopLine"];
    _showTopLine = showTopLine;
    [self didChangeValueForKey:@"showTopLine"];
    self.topLineView.hidden = !showTopLine;
    [self setNeedsLayout];
}

- (void)setShowBottomLine:(BOOL)showBottomLine {
    [self willChangeValueForKey:@"showBottomLine"];
    _showBottomLine = showBottomLine;
    [self didChangeValueForKey:@"showBottomLine"];
    self.bottomLineView.hidden = !showBottomLine;
    [self setNeedsLayout];
}
#pragma mark -

#pragma mark - getter
- (UIView *)topLineView {
    if(!_topLineView) {
        _topLineView = [[UIView alloc] initWithFrame:CGRectMake(_topHeadIndentationWidth, 0, self.width - _topHeadIndentationWidth, 1.0f / ZallDeviceScale())];
        _topLineView.backgroundColor = UIColorFromHexRGB(0xdddddd);
        [self.contentView addSubview:_topLineView];
    }
    return (_topLineView);
}

- (UIView *)bottomLineView {
    if(!_bottomLineView) {
        _bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(_bottomHeadIndentationWidth, 0, self.width - _bottomHeadIndentationWidth, 1.0f / ZallDeviceScale())];
        _bottomLineView.backgroundColor = UIColorFromHexRGB(0xdddddd);
        [self.contentView addSubview:_bottomLineView];
    }
    return (_bottomLineView);
}
#pragma mark -

@end
